#ifndef CLUSNET_BASE_DYNAMICBUFFERLOW_H
#define CLUSNET_BASE_DYNAMICBUFFERLOW_H

#include <vector>
#include <cstddef>
#include <algorithm>
#include <iterator>

namespace clusnet
{
namespace base
{

class DynamicBufferLow
{
    public:
        DynamicBufferLow(const DynamicBufferLow& that) = default;
        DynamicBufferLow(DynamicBufferLow&& that) = default;
        DynamicBufferLow& operator= (const DynamicBufferLow& that) = default;
        DynamicBufferLow& operator= (DynamicBufferLow&& that) = default;
        explicit DynamicBufferLow(size_t initialSize = kInitialSize)
            : data_()
        {
            data_.reserve(initialSize);
        }
        virtual ~DynamicBufferLow() = default;

    public:
        void append(const char* buf, size_t len)
        {
            std::copy_n(buf, len, std::back_inserter(data_));
        }

        size_t length() const { return data_.size(); }

        void reset() { data_.clear(); }
        void bzero() { std::fill_n(data_.begin(), data_.size(), '\0'); }

        std::string toString() const { return std::string(data_.cbegin(), data_.cend()); }

    private:
        static const size_t kInitialSize = 1024;
        std::vector<char>   data_;      // 性能比直接用malloc分配内存要慢一个数量级
};
}
}

#endif 
